
//
//  NetworkManager+Extensions.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/23.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import Foundation

extension NetworkManager {
    func statusList(since_id: Int64=0, max_id: Int64=0, completion:@escaping (_ list: [[String: Any]]?, _ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        https://rm.api.weibo.com/2/remind/unread_count.json
//        let params = ["access_token": "2.00piw1VCLlbpYB34c0a5945d0AxCSb"]
        let params = ["since_id": "\(since_id)",
            "max_id":"\(max_id>0 ? max_id-1 : 0)"]
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
//            let result = json?["statuses"] as? [[String: Any]]
//            let resutl = json?["statuses"] as? [[String: Any]]
//            print(json?["statuses"] ?? "")
            let result = json as? [String: Any]
            let res = result?["statuses"] as? [[String: Any]]
            completion(res, isSuccess)
        }
    }
    
    func unreadCount(completion:@escaping (_ count: Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            let dict = json as? [String: Any]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
        }   
    }
}

extension NetworkManager {
    
    func postStatus(text: String, completion:@escaping (_ result:[String: Any]?, _ isSuccess: Bool)->()) -> () {
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let params = ["status" : text]
        
        tokenRequest(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            completion(json as? [String: Any], isSuccess)
        }
        
    }
    
}


extension NetworkManager {
    
    func loadUserInfo(completion:@escaping (_ dict: [String:Any])->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
//            print(json ?? "")
            completion(json as? [String:Any] ?? [:])
        }
    }
    
}

extension NetworkManager {
    
    func loadAccessToken(code:String, completion:@escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id":AppKey,
                      "client_secret":AppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":RedirectURI]
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            self.userAccount.yy_modelSet(with: (json as? [String:Any]) ?? [:])
            self.loadUserInfo(completion: { (dict) in
                self.userAccount.yy_modelSet(with: dict)
                self.userAccount.saveAccount()
                
                completion(isSuccess)
            })
        }
    }
    
}
